package com.bnzj.cloud.business.core.persistence.repository;

import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Repository
public interface SysUserRepository extends BaseRepository<SysUser, Long> {

    /**
     * 登录专用，非懒加载模式
     *
     * @param account
     * @return
     */
    @Query(value = "from #{#entityName} u LEFT JOIN FETCH u.roleUserList ru LEFT JOIN FETCH ru.role where u.personnel = '1' and u.enabled = '1' and u.account=?1")
    SysUser eagerlyFindByAccount(String account);

    @Query(value = "from #{#entityName} where personnel = 1 and enabled = 1 and account=?1")
    SysUser findByAccount(String account);

    /**
     * 查询全部在职且启用的员工的工号
     *
     * @return
     */
    @Query("select account from #{#entityName} where account != 'admin'")
    Set<String> findAllAccount();

    /**
     * 查询全部在职且启用的员工的工号
     *
     * @return
     */
    @Query("select account from #{#entityName} where personnel = 1 and enabled = 1 and account != 'admin'")
    Set<String> findAllAccountByEnabled();

    @Query("from #{#entityName} where account in (?1)")
    List<SysUser> findAllByAccount(List<String> accoutList);

    @Modifying
    @Query("update #{#entityName} set enabled = 0, personnel = 0 where account != 'admin' and account in (?1)")
    int disable(List<String> accoutList);

    @Query("select id from #{#entityName} where account = ?1")
    Long findIdByAccount(String account);

    @Modifying
    @Query("update #{#entityName} set enabled = (?2) where account != 'admin' and id = (?1)")
    void updateUserEnableById(Long id, boolean sign);

    @Query(value = "SELECT email FROM #{#entityName} where account=?1")
    String findEmailByAccount(String account);

    @Override
    @Query(value = "delete from bnz_sys_user where id = ?1", nativeQuery = true)
    void deleteById(Long id);


    @Query(value = "SELECT  su.* FROM bnz_sys_user su  WHERE " +
            " EXISTS ( SELECT sru.user_id FROM bnz_sys_role_user sru WHERE sru.role_id = ?1 AND sru.user_id = su.id )", nativeQuery = true)
    List<SysUser> findUsersByRoleId(Long roleId);

    @Query(value = "SELECT  su.* FROM bnz_sys_user su  WHERE " +
            " EXISTS ( SELECT sru.user_id FROM bnz_sys_role_user sru WHERE sru.role_id = ?1 AND sru.user_id = su.id ) and su.type='A'", nativeQuery = true)
    List<SysUser> findBaseUsersByRoleId(Long roleId);

    @Modifying
    @Query(value = "insert into bnz_sys_user (id, account, cname, email, phone, dept_id,full_dept_name,position,enabled,type) "
            + "values (?#{[0].id}, ?#{[0].account}, ?#{[0].cname}, ?#{[0].email},?#{[0].phone},?#{[0].deptId},?#{[0].fullDeptName},?#{[0].position},  "
            + "?#{[0].enabled}, ?#{[0].type})", nativeQuery = true)
    void newSave(SysUser user);


    @Modifying
    @Query(value = "update bnz_sys_user set password =:password where account =:account and  account != 'admin'", nativeQuery = true)
    void updatePassword(@Param("password") String password, @Param("account") String account);
    
    @Query(value = "SELECT * FROM bnz_sys_user where idk3=?1",nativeQuery = true)
    SysUser findByIdk3(String idk3);


    List<SysUser> findAllByLinkIdIn(List<Long> supplierIds);

    List<SysUser> findAllByTypeAndEnabled(String type,Boolean enabled);
    
    @Query("select idk3 from #{#entityName} where personnel = ?1 AND ISNULL(idk3) = 0")
    List<String> findCodeByPersonnel(Boolean personnel);
    
    @Modifying
    @Query("update #{#entityName} set personnel = 0, modifyBy = ?2, modifyName = ?3, modifyTime = ?4 where idk3 = ?1")
    void disPersonnel(String idk3, String modifyBy, String modifyName, Date modifyTime);
}
